package com.cafebabe.mongo.service.impl;

import com.cafebabe.bean.MongoQuery;
import com.cafebabe.bean.Pager;
import com.cafebabe.callback.LogAction;
import com.cafebabe.mongo.dao.MongoDao;

import com.cafebabe.mongo.entity.MongoEntity;
import com.cafebabe.mongo.service.MongoService;
import com.cafebabe.mongo.service.bean.ResultService;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;


@Service
public abstract class MongoServiceImpl<T extends MongoEntity, PK extends Serializable> implements MongoService<T,PK> {

    private MongoDao<T, PK> mongoBaseDao;

    @Resource
    protected ResultService resultService;

    public abstract MongoDao<T, PK> getMongoBaseDao();

    public void setMongoBaseDao(MongoDao<T, PK> mongoBaseDao) {
        this.mongoBaseDao = mongoBaseDao;
    }

    @Override
    public List<T> find(Query query) {
        return getMongoBaseDao().find(query);
    }

    @Override
    public T findOne(String query) {
        return getMongoBaseDao().findOne(query);
    }

    @Override
    public void update(Query query, Update update) {
        getMongoBaseDao().update(query,update);
    }

    @Override
    public T save(T entity) {
        return getMongoBaseDao().save(entity);
    }

    @Override
    public T findById(String id) {
        return getMongoBaseDao().findById(id);
    }

    @Override
    public T findById(String id, String collectionName) {
        return getMongoBaseDao().findById(id,collectionName);
    }

    @Override
    public Pager findPage(Pager pager, Query query) {
        return getMongoBaseDao().findPage(pager,query);
    }

    @Override
    public Pager findPagerByMap(Pager pager, Map<String, Map<String, Object>> smap,LogAction logAction) {

        pager =  getMongoBaseDao().findPagerByMap(pager, smap);
        logAction.toJson(pager);
        return pager;
    }

    @Override
    public Pager findPager(Pager pager ,List<MongoQuery> mongoQueries) {
        return getMongoBaseDao().findPager(pager,mongoQueries);
    }


    @Override
    public long count(Query query) {
        return getMongoBaseDao().count(query);
    }



}
